<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class ext_kerjasama_usaha extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('ext_kerjasama_usaha_model', 'Kerjasama');
		$this->load->model('combo_model', 'Combo');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Kerjasama Usaha";
		
		// Define Combo Data
		$data['arr_application']	= $this->Combo->ITApplication();
		
		viewByTemplate('ext_kerjasama_usaha/index', $data, 'grid');
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->Kerjasama->GetJSON();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation
		
		if (post("kode_kerjasama") == '') {
			$error->Add("kode_kerjasama", "Kode Kerjasama", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM ext_kerjasama_usaha WHERE kode_kerjasama = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_kerjasama"))))."'")) > 0) {
			$error->Add("kode_kerjasama", "Kode Kerjasama", $tpl_duplicate);
		}

		if (post("nama_kerjasama") == '') {
			$error->Add("nama_kerjasama", "Nama Kerjasama", $tpl_empty);
		}

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	= post_by_key(array('kode_kerjasama', 'nama_kerjasama', 'harga', 'app_id', 'keterangan'));
				$data['kode_kerjasama']	= @ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim($data['kode_kerjasama'])));
				
				if (post("mode") == "create")
				{
					$this->Kerjasama->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					
					$this->Kerjasama->Update($data, 
										array('kode_kerjasama'	=> $data['kode_kerjasama']));

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$kode_kerjasama	= get('kode_kerjasama');
		
		if ($kode_kerjasama != "")
		{
			// Delete Single
			$arr_rule	= array('kode_kerjasama' => $kode_kerjasama);
			$ls_result = $this->Kerjasama->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('kode_kerjasama' => $row['kode_kerjasama']);
				$ls_result .= $this->Kerjasama->DeleteSingle($arr_rule);
			}
		}
		
		echo $ls_result;
	}
}
?>